from API.okex import get_usdtusd_premium_okex
import os
import pandas as pd
from utils import df_into_db


# 从okex获取不同频率的usdtusd溢价数据，每日更新
DATA_DIR = 'E:\\data'
PREMIUM_DATA_DIR = os.path.join(DATA_DIR, 'usdt_premium')
print('更新不同频率的usdtusd溢价数据')
for frequency in ['5m', '1h', '1d']:
    update = get_usdtusd_premium_okex(frequnecy=frequency)
    file_name = os.path.join(PREMIUM_DATA_DIR, f'usdtusd_premium_{frequency}_okex.csv')
    if os.path.exists(file_name):
        historical_df = pd.read_csv(file_name)
        if max(historical_df['datetime']) < max(update['datetime']):
            df = pd.concat([historical_df, update], axis=0, ignore_index=True)
            df.drop_duplicates(subset=['datetime'], keep='first', inplace=True)
            df["frequency"] = frequency
            # df.to_csv(file_name, index=False)

            df_into_db(df, db_name="usdt_premium", table_name="usdtusd_premium_okex")
    else:
        # update.to_csv(file_name, index=False)
        pass